﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace GDFlacTools.Controls.ComboBoxCustom
{
    public partial class ComboBoxCustom : ComboBox
    {
        public ComboBoxCustom()
        {
            InitializeComponent();
            DrawMode = DrawMode.OwnerDrawFixed;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            if (e.Index < 0) { return; }
            e.DrawBackground();
            var item = (ComboBoxItem)Items[e.Index];
            Brush brush = new SolidBrush(item.ForeColor);
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            { brush = Brushes.White; }
            e.Graphics.DrawString(item.Text, Font, brush, e.Bounds.X, e.Bounds.Y);
        }

        object _selectedValue = null;
        public new Object SelectedValue
        {
            get
            {
                object ret = null;
                if (SelectedIndex >= 0)
                {
                    ret = ((ComboBoxItem)SelectedItem).Value;
                }
                return ret;
            }
            set { _selectedValue = value; }
        }

        string _selectedText = string.Empty;
        public new String SelectedText
        {
            get
            {
                return ((ComboBoxItem)this.SelectedItem).Text;
            }
            set { _selectedText = value; }
        }
    }

    public class ComboBoxItem
    {
        public ComboBoxItem() { }

        public ComboBoxItem(string pText, object pValue)
        {
            _text = pText; _val = pValue;
        }

        public ComboBoxItem(string pText, object pValue, Color pColor)
        {
            _text = pText; _val = pValue; _foreColor = pColor;
        }

        string _text = "";
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        object _val;
        public object Value
        {
            get { return _val; }
            set { _val = value; }
        }

        Color _foreColor = Color.Black;
        public Color ForeColor
        {
            get { return _foreColor; }
            set { _foreColor = value; }
        }

        public override string ToString()
        {
            return _text;
        }
    }
}
